<?php

namespace backend\models;

use Yii;
use common\models\UserInfo;
use yii\helpers\ArrayHelper;
use yii\db\ActiveRecord;
use backend\models\Qr;
use backend\models\PayTrade;
use yii\data\ActiveDataProvider;
use yii\swiftmailer\Message;


class CrowdItemPrice extends \yii\db\ActiveRecord
{
	
	public $m_name;
	public $f_name;
	public $s_name;
	public $m_url;
	public $f_url;
	public $tel;
	public $username;
	public $nickname;
	public $price_state;
	public $price_modify;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'crowd_price';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {    	
        return [
            [['id','size','crowd_id','exceed_limit','order_count','preview_id'], 'integer'],
        	[['crowd_id'],'required'],
            [['preview_id'],'required','message'=>'请选择正确的单品规格 ! '],
        	[['price'],'number']	 ,
            [['price_limit'],'integer'],
        	[['tips','send_time'],'safe']
        ];
    }
    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'crowd_id' => '众筹id',
            'preview_id'=>Yii::t('app','单品规格状态(自动填充，无需手动输入)'),
            'm_name' => Yii::t('app','材质'),
            'f_name' => Yii::t('app','面料'),
        	's_name'=>Yii::t('app','尺寸'),
            'm_url' => Yii::t('app','材质图片'),
            'f_url' => Yii::t('app','面料图片'),
            'size' => Yii::t('app','尺寸'),
            'tips' => Yii::t('app','描述'),
        	'price' => Yii::t('app','众筹价'),
        	'price_limit' => Yii::t('app','限额数量'),
        	'exceed_limit' => Yii::t('app','是否可超过限额'),
        	'order_count' => Yii::t('app','实际下单数'),
        	'send_time' => Yii::t('app','预计发货时间'),
        ];
    }
   /**
    * 根据众筹id获取众筹规格列表
    */
    
    public function getCrowdItem(){
        return $this->hasOne(CrowdItem::className(),['id'=>'crowd_id']);
    }
  public function getPriceByCrowdid($crowdid){
  	
  	
/*   	$sql="			 
	 SELECT   p.id,p.`crowd_id`,p.price,p.price_limit,p.exceed_limit,p.order_count,
 	p.send_time ,m.`name` m_name,m_image.url m_url,f.`name` f_name, f_image.url f_url, sys_item_size.size s_name
    FROM crowd_price  p
    left join    crowd_item on crowd_item.id=p.crowd_id 
    left join    base_sys_preview on base_sys_preview.id=p.preview_id
	LEFT JOIN base_sys_material m ON  m.`id` IN  (SELECT `mid` FROM  base_sys_preview WHERE id=p.`preview_id`)
	LEFT JOIN base_sys_fabric f ON  f.id IN (SELECT `fid` FROM  base_sys_preview WHERE id=p.`preview_id`)
  	LEFT JOIN sys_item_size ON sys_item_size.id=p.size
  	LEFT JOIN sys_image m_image ON m_image.`id`=m.`img_small_id`
	LEFT JOIN sys_image f_image ON f_image.id=f.`img_small_id`
	WHERE p.`crowd_id`=:crowd_id  and base_sys_preview.item_id=crowd_item.item_id"; */
  	
  	$sql=" SELECT   p.id,crowd_item.item_id,p.`crowd_id`,p.price,p.price_limit,p.exceed_limit,p.order_count,p.`preview_id`,
 	p.send_time ,m.`name` m_name,m_image.url m_url,f.`name` f_name, f_image.url f_url, sys_item_size.size s_name
    FROM crowd_price  p 
	LEFT JOIN crowd_item ON crowd_item.id=p.`crowd_id` 
	LEFT JOIN base_sys_material m ON  m.`id` IN 
   (SELECT `mid` FROM  base_sys_preview  WHERE id =p.`preview_id` AND item_id=crowd_item.item_id )
	LEFT JOIN base_sys_fabric f ON  f.id IN 
    (SELECT  fid  FROM  base_sys_preview   WHERE id=p.`preview_id` AND item_id=crowd_item.item_id)
  	LEFT JOIN sys_item_size ON sys_item_size.id=p.size
  	LEFT JOIN sys_image m_image ON m_image.`id`=m.`img_small_id`
	LEFT JOIN sys_image f_image ON f_image.id=f.`img_small_id`
	WHERE p.`crowd_id`=:crowd_id    
	";
  	//如果面料 材质 尺寸被修改 则不显示  AND (f.`name`!='' OR m.name!='')
  	$param=array(":crowd_id"=>$crowdid);
  	$crowdpriceList=CrowdItemPrice::findBySql($sql,$param);
  	$dataProvider = new ActiveDataProvider([
  			'query' => $crowdpriceList,
  			'sort'=>false
  	]);
  	
  	return $dataProvider;
  }
  
  /**
   * 根据previewid获取面料 材质  尺寸
   */
  public function getpreview($previewId,$sid){
          $crowdItemModel=new CrowdItem();
          $sql="SELECT fid,mid,item_id FROM base_sys_preview   WHERE id=:previewid";
          $param=array(":previewid"=>$previewId);
          $preview=ItemPreview::findBySql($sql,$param)->asArray()->one();
          $fid=$preview['fid'];
          $mid=$preview['mid'];
          $item_id=$preview['item_id'];
          $html="";
          if(intval($item_id)==0){
              return "<div>单品规格被删除，无法编辑!</div>";
          }
          //无面料
          if(intval($fid)==0){
              $html.="<div class='fm_list'><div class='fabirc'><h6>暂无面料</h6> </div>";
               if(intval($mid)==0){//一般不存在这种情况
                   $html.="<div class='mabir'><h6>暂无材质</h6></div>";
               }else{ 
                   //查询当前单品的材质
                   $html.=$this->getMlistByitemid($item_id,$mid,$sid);
               }
          //有面料     
          }else{
              $html.="<div class='fm_list'><div class='fabirc'>";
              $html.=$this->getFMlistByFiditemid($item_id,$fid,$mid,$sid);
          }
          return $html;
   }
      
      /**
       * 根据面料  单品 获取材质
       * @param   $item_id
       * @param   $fid
       * @return string
       */
   function getFMlistByFiditemid($item_id,$fid,$mid,$sid){
          $crowdItemModel=new CrowdItem();
          $f_list=$crowdItemModel->getFabricByItem($item_id,0);
          $m_list=$crowdItemModel->getMaterialByItem($item_id,$fid);
          $html='';
          if(count($m_list)>0){//存在材质 显示材质，根据材质显示尺寸
              foreach($f_list as $fari){
                  if($fid==$fari['fid']){
                      $html.=$fari['name']." &nbsp;&nbsp;
    				<input type='radio' name='CrowdItem[fid]' value='".$fari['fid']."' onchange='fabricChange(this)' checked='checked'>";
                      $html.="<img  src='".$fari['url']."' width=50px;/>";
                      $html.="&nbsp;&nbsp;&nbsp;&nbsp;";
                  }else{
                      $html.=$fari['name']." &nbsp;&nbsp;
    				<input type='radio' name='CrowdItem[fid]' value='".$fari['fid']."' onchange='fabricChange(this)'>";
                      $html.="<img  src='".$fari['url']."' width=50px;/>";
                      $html.="&nbsp;&nbsp;&nbsp;&nbsp;";
                  }
              }
              $html.="
   			 </div>";
              $html.=$this->getMlistByitemid($item_id,$mid,$sid );
             // $html.=$this->getSizeListBymid($item_id,$fid,$mid,$sid);
          }else{
              //根据item_id 面料  获取尺寸 如果sys_item_prices没有尺寸 还需要去sys_item_size查找尺寸
              foreach($f_list as $fari){
                  if($fid==$fari['fid']){
                      $html.=$fari['name']." &nbsp;&nbsp;<input type='radio' name='CrowdItem[fid]' value='".$fari['fid']."' onchange='f_getSize(this)' checked='checked'>";
                      $html.="<img  src='".$fari['url']."' width=50px;/>";
                      $html.="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                  }else{
                      $html.=$fari['name']." &nbsp;&nbsp;<input type='radio' name='CrowdItem[fid]' value='".$fari['fid']."' onchange='f_getSize(this)'>";
                      $html.="<img  src='".$fari['url']."' width=50px;/>";
                      $html.="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                  }
              }
              $html.="
   			 </div>";
              $html.="<div class='mabir'>  <h6> 暂无材质</h6></div>";
              $html.=$this->getSizeListBymid($item_id,$fid,$mid,$sid);
              $html.="
    			 ";
            }
            return $html;
      }
      /**
       * 根据item_id   获取材质  无面料 
       */
      function getMlistByitemid($item_id,$mid,$sid){
          $crowdItemModel=new CrowdItem();
          $m_list=$crowdItemModel->getMaterialByItem($item_id,0);
          $m_count=intval(count($m_list));
          $html='';
          if($m_count>0){//存在材质 显示材质，根据材质显示尺寸
              $html.="<div class='mabir'><h6> 材质：</h6>";
              $m_list=$crowdItemModel->getMaterialByItem($item_id,0);
              foreach($m_list as $fari){
                  if($mid==$fari['mid']){
                      $html.=$fari['name']." &nbsp;&nbsp;
	    		                     <input type='radio' name='CrowdItem[mid]' value='".$fari['mid']."'
	    				            onchange='m_getSite(this)' checked='checked'>";
                      $html.="<img  src='".$fari['url']."' width=50px;/>";
                      $html.=" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                  }else{
                      $html.=$fari['name']." &nbsp;&nbsp;
	    		                     <input type='radio' name='CrowdItem[mid]' value='".$fari['mid']."'
	    				            onchange='m_getSite(this)'>";
                      $html.="<img  src='".$fari['url']."' width=50px;/> ";
                      $html.="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                  }
              }
              $html.="</div><div class='size'>";
              //查询当前材质下的尺寸列表
              $html.=$this->getSizeListBymid($item_id,0,$mid,$sid);
          }else{
              //没有面料也没有材质的情况，直接显示尺寸   这种情况是不存在的
              $html.="<div class='mabir'> <h6> 暂无材质：</h6></div><div class='size'> <h6>暂无尺寸 </h6>";
          }
          return $html;
      }
      /**
       * 根据item_id  mid获取尺寸
       * @param unknown $item_id
       * @param unknown $mid
       * @return string
       */
      function   getSizeListBymid($item_id,$fid,$mid,$sid){
          $crowdItemModel=new CrowdItem();
          $sizeList=$crowdItemModel->getSizeByItemFm($item_id,0,$mid);
          if(count($sizeList)==0){
              //如果没有尺寸  就查找size表
              $sizeList=$crowdItemModel->getSizeByItem($item_id);
              if(count($sizeList)>0){
                  $html=" <h6> 尺寸：</h6>";
                  foreach($sizeList as $size){
                      if($sid==$size['id']){
                          $html.=$size['size']."
	    		                    <input type='radio' name='CrowdItem[size]' value='".$size['id']."'
	    				            onchange='getprice(".$item_id.",".$fid.",".$mid.",".$size['id'].")' checked='checked'>";
                          $html.=" &nbsp;&nbsp; ";
                      }else{
                          $html.=$size['size']."
	    	                  	    <input type='radio' name='CrowdItem[size]' value='".$size['id']."'
	    				            onchange='getprice(".$item_id.",".$fid.",".$mid.",".$size['id'].")'>";
                          $html.=" &nbsp;&nbsp; ";
                      }
                  }
              }else{
                  $html=" <h6>暂无尺寸 </h6> "; 
              }
          }else{
              $html=" <h6> 尺寸：</h6>";
              foreach($sizeList as $size){
                  if($sid==$size['id']){
                      $html.=$size['size']." &nbsp;&nbsp;
		    		                <input type='radio' name='CrowdItem[size]' value='".$size['id']."'
		    				            onchange='getprice(".$item_id.",".$fid.",".$mid.",".$size['id'].")' checked='checked'>";
                      $html.="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                  }else{
                      $html.=$size['size']." &nbsp;&nbsp;
		    		                <input type='radio' name='CrowdItem[size]' value='".$size['id']."'
		    				            onchange='getprice(".$item_id.",".$fid.",".$mid.",".$size['id'].")'>";
                      $html.="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ";
                  }
              }
          }
          $html.="</div>";
          return $html;
          
      }
      
}
